/*
 * Copyright (c) 2010 MoSync AB
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include <maapi.h>
#include <conprint.h>

#include "AbstractInterpreter.h"
#include "System.h"

namespace MAInterpreter
{

// ========== Constructor/Destructor ==========

AbstractInterpreter::AbstractInterpreter() :
	dispatchEvents(true),
	interpreterErrorListener(NULL)
{
	setScreenLeft(0);
	setScreenTop(0);
	setScreenWidth(Sys()->getScreenWidth());
	setScreenHeight(Sys()->getScreenHeight());
}

AbstractInterpreter::~AbstractInterpreter()
{
}

// ========== Methods ==========

/**
 * Set if events should be dispatched.
 */
void AbstractInterpreter::setDispatchEvents(bool dispatch)
{
	dispatchEvents = dispatch;
}

/**
 * Get if events should be dispatched.
 */
bool AbstractInterpreter::getDispatchEvents()
{
	return dispatchEvents;
}

void AbstractInterpreter::setInterpreterErrorListener(
	InterpreterErrorListener* listener)
{
	interpreterErrorListener = listener;
}

void AbstractInterpreter::reportInterpreterError(const char* errorMessage)
{
	if (NULL != interpreterErrorListener)
	{
		interpreterErrorListener->onInterpreterError(errorMessage);
	}
}

/**
 * Main event loop. This event loop does not support downloaders,
 * therefore the event loop in class Moblet should be used instead.
 */
/*
void AbstractInterpreter::mainLoop()
{
	MAEvent event;

	int lastClockTick = maGetMilliSecondCount();

	// Call OnInit() and first time OnDraw()
	onInit();
	onDraw();

	while (1)
	{
		maWait(0);
		while (maGetEvent(&event))
		{
			switch (event.type)
			{
				case EVENT_TYPE_CLOSE:
					return;
				case EVENT_TYPE_POINTER_PRESSED:
					onTouchDown(event.point.x, event.point.y);
					break;
				case EVENT_TYPE_POINTER_RELEASED:
					onTouchUp(event.point.x, event.point.y);
					break;
				case EVENT_TYPE_POINTER_DRAGGED:
					onTouchDrag(event.point.x, event.point.y);
					break;
				case EVENT_TYPE_KEY_PRESSED:
					onKeyDown(event.key);
					break;
				case EVENT_TYPE_KEY_RELEASED:
					onKeyDown(event.key);
					break;
				// TODO: Implement async download of files.
				// Currently, class Moblet is used for this.
				//case EVENT_TYPE_CONN:
				//	environment->fireConnEvent(event.conn);
				//	break;
				//case EVENT_TYPE_CONN:
				//	if(data.opType == CONNOP_FINISH)
				//		((HttpConnectionListener*)mListener)->httpFinished(this, data.result);
				//	onConnection(event.conn.result);
				//	break;
			}
		}

		// Handle clock ticks.
		// TODO: Should use maWait here to not hog the system!
		int millisecondInterval = getClockMillisecondInterval();
		if (millisecondInterval > -1)
		{
			int now = maGetMilliSecondCount();
			if (now >= lastClockTick + millisecondInterval)
			{
				// Save timestamp for this tick.
				lastClockTick = now;

				// Call OnClockTick() and OnDraw()
				onClockTick();
				onDraw();
			}
		}
	}
}
*/

}
